#ifndef SIMULATION_MANAGER
#define SIMULATION_MANAGER

#include <sdk_global.h>

#include <QObject>
#include <QList>
#include <QString>
#include <SimulationObject.h>
#include <QSharedPointer>
#include <QTimer>
#include <QThread>
#include <OpenThreads/Thread>
#include <OpenThreads/Condition>
#include <OpenThreads/Mutex>
#include <OpenThreads/Block>
#include <QDateTime>

QT_BEGIN_NAMESPACE

class QDESIGNER_SDK_EXPORT SimulationManager: public QObject
{
    Q_OBJECT
public:

    SimulationManager();
    virtual ~SimulationManager();

    void addSimulationObject(QSharedPointer<SimulationObject> simObj);
    void setRunAsSingleThread();
    void setRunInMainThread();

    void waitUpdateEndAndStopSim();
    void stopWaitUpdateEnd();

    bool getIsWaitFrameEnd();

    virtual void update();

    void startSimulation();
    void stopSimulation();
    void pauseSimulation();


public slots:
    void slotTimer();
    //void doWork();
protected:
    void beforStart();
    void running();
    void pausing();

    void callSimObjListUpdatePausingToUpdate();
    void callSimObjListUpdateRunningToUpdate();

    void callSimObjListUpdateToUpdateRunning();
    void callSimObjListUpdatePausingToUpdateRunning();

    void callSimObjListUpdateToUpdatePausing();
    void callSimObjListUndateRunningToUpdatePausing();

    void callSimObjListUpdate();
    void callSimObjListUpdateRunning();
    void callSimobjListUpdatePausing();
private:
    QList<QSharedPointer<SimulationObject>> m_simObjList;
    QTimer* m_timer;
    //QThread* m_thread;
    OpenThreads::Thread* m_thread;
    //OpenThreads::Block* m_block;
    bool m_waitFrameEnd;
    int m_currentMode;//0, timer mode, 1, thread mode, -1 no mode;

    double m_timerSecond;

    bool m_buttonStart;
    bool m_buttonPause;
    bool m_buttonStop;

    int m_state;//1,updating, 2,updateRunning, 3, updatePausing,
    int m_timerCount;

    QDateTime m_preTime;
};

QT_END_NAMESPACE

#endif // SIMULATION_MANAGER
